package _18_剑指OfferII;

public class _095_剑指OfferII最长公共子序列 {

    public static void main(String[] args) {

        _095_剑指OfferII最长公共子序列 v = new _095_剑指OfferII最长公共子序列();

        String str1 = "abc";
        String str2 = "def";
        System.out.println(v.longestCommonSubsequence(str1, str2));

    }

    public int longestCommonSubsequence(String text1, String text2) {
        char[] chars1 = text1.toCharArray();
        char[] chars2 = text2.toCharArray();
        int len1 = chars1.length;
        int len2 = chars2.length;
        int[][] dp = new int[len1 + 1][len2 + 1];

        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (chars1[i - 1] == chars2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[len1][len2];
    }

}
